package com.oa.srms.apps.business.organization.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.oa.srms.apps.business.organization.IPostCategoryService;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.organization.IPostCategoryDao;
import com.oa.srms.apps.entity.organization.PostCategoryValue;

public class PostCategoryServiceImpl implements IPostCategoryService {
	@Autowired
	private PostCategoryValue postCategory;
	@Autowired
	private IPostCategoryDao categoryDao;

	@Override
	public int addPostCategory(String categoryName) throws Exception {
		postCategory.setCategoryName(categoryName);
		postCategory.setStates(IDEDStates.ENABLE);
		return categoryDao.addPostCategory(postCategory);
	}

	@Override
	public boolean deletePostCategory(String categoryIds) throws Exception {
		String[] categoryIdStr = categoryIds.split(",");
		for(String categoryId : categoryIdStr){
			postCategory = queryByCategoryId(Integer.parseInt(categoryId));
			if(postCategory != null){
				postCategory.setStates(IDEDStates.DELETE);
				categoryDao.editPostCategory(postCategory);
			}else{
				return false;
			}
		}
		return true;
		
	}

	@Override
	public boolean modifyPostCategory(int categoryId, String categoryName)
			throws Exception {
		postCategory = queryByCategoryId(categoryId);
		if(postCategory != null){
			postCategory.setCategoryName(categoryName);
			return categoryDao.editPostCategory(postCategory);
		}
		return false;
	}

	@Override
	public List<PostCategoryValue> queryALLCategoryInfo() throws Exception {
		return categoryDao.queryPostCategory(postCategory);
	}

	@Override
	public PostCategoryValue queryByCategoryId(int categoryId) throws Exception {
		postCategory.setCategoryId(categoryId);
		return categoryDao.queryByCategoryId(postCategory);
	}

	@Override
	public List<PostCategoryValue> queryPostCategoryByStates(Integer states)
			throws Exception {
		postCategory.setStates(states);
		return categoryDao.queryPostCategory(postCategory);
	}

	@Override
	public boolean toDisable(int categoryId) throws Exception {
		postCategory = queryByCategoryId(categoryId);
		if(postCategory != null){
			postCategory.setStates(IDEDStates.DISABLE);
			return categoryDao.editPostCategory(postCategory);
		}
		return false;
	}

	@Override
	public boolean toEnable(int categoryId) throws Exception {
		postCategory = queryByCategoryId(categoryId);
		if(postCategory != null){
			postCategory.setStates(IDEDStates.ENABLE);
			return categoryDao.editPostCategory(postCategory);
		}
		return false;
	}

}
